{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-12-25\n"
     ]
    }
   ],
   "source": [
    "class MyDate:\n",
    "    def __init__(self, year, month, day):\n",
    "        self.year = year\n",
    "        self.month = month\n",
    "        self.day = day\n",
    "\n",
    "    def __repr__(self):\n",
    "        return f\"{self.year}-{self.month}-{self.day}\"\n",
    "\n",
    "    # 实例方法\n",
    "    def add_days(self, days):\n",
    "        self.day += days\n",
    "    \n",
    "    # 静态方法，适合不关于类的操作\n",
    "    @staticmethod\n",
    "    def parse_date_string(date_str):\n",
    "        year, month, day = date_str.split('-')\n",
    "        # 硬编码了，如果类名改变了，这里也要修改\n",
    "        return MyDate(year, month, day)\n",
    "    \n",
    "    # 类方法，可以访问类的属性和方法\n",
    "    @classmethod\n",
    "    def from_string(cls, date_str):\n",
    "        year, month, day = date_str.split('-')\n",
    "        return cls(year, month, day)\n",
    "    \n",
    "    @staticmethod\n",
    "    def is_leap_year(year):\n",
    "        if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):\n",
    "            print(f\"{year} is a leap year\")\n",
    "        else:\n",
    "            print(f\"{year} is not a leap year\")\n",
    "date = MyDate(2021, 12, 25)\n",
    "print(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-12-26\n"
     ]
    }
   ],
   "source": [
    "date.add_days(1)\n",
    "print(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-01-01\n"
     ]
    }
   ],
   "source": [
    "d2 = MyDate.parse_date_string(\"2021-01-01\")\n",
    "print(d2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-09-01\n"
     ]
    }
   ],
   "source": [
    "d3 = MyDate.from_string(\"2021-09-01\")\n",
    "print(d3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020 is a leap year\n"
     ]
    }
   ],
   "source": [
    "MyDate.is_leap_year(2020)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "grammer-study-12-env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
